package com.example.labSystem.controller;

import com.example.labSystem.entity.User;
import com.example.labSystem.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;

import java.util.List;

/**
 * <p>
 * 用户信息表 前端控制器
 * </p>
 *
 * @author 10
 * @since 2024-12-12
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private IUserService userService;

    /**
     * 用户登录
     */
    @PostMapping("/login")
    public ResponseEntity<?> login(@RequestBody User user) {
        User loggedInUser = userService.login(user.getUsername(), user.getPassword());
        if (loggedInUser != null) {
            return ResponseEntity.ok(loggedInUser);
        }
        return ResponseEntity.status(HttpStatus.UNAUTHORIZED).body("用户名或密码错误");
    }


    /**
     * 获取所有用户信息
     */
    @GetMapping("/list")
    public ResponseEntity<List<User>> getAllUsers() {
        List<User> users = userService.list();
        return ResponseEntity.ok(users);
    }

    /**
     * 根据用户ID获取用户信息
     */
    @GetMapping("/{id}")
    public ResponseEntity<User> getUserById(@PathVariable("id") String id) {
        User user = userService.getById(id);
        return user != null ?
                ResponseEntity.ok(user) :
                ResponseEntity.status(HttpStatus.NOT_FOUND).build();
    }

    /**
     * 根据真实姓名查询用户信息
     */
    @GetMapping("/searchByFullName")
    public ResponseEntity<List<User>> getUserByFullName(@RequestParam("fullName") String fullName) {
        List<User> users = userService.getUsersByFullName(fullName);
        return users.isEmpty() ?
                ResponseEntity.status(HttpStatus.NOT_FOUND).build() :
                ResponseEntity.ok(users);
    }


    /**
     * 添加新用户
     */
    @PostMapping("/add")
    public ResponseEntity<String> addUser(@RequestBody User user) {
        if (userService.isUsernameExists(user.getUsername())) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户名已存在");
        }
        boolean isAdded = userService.save(user);
        return isAdded ?
                ResponseEntity.status(HttpStatus.CREATED).body("添加成功") :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).body("添加失败");
    }

    /**
     * 更新用户信息
     */
    @PutMapping("/update")
    public ResponseEntity<String> updateUser(@RequestBody User user) {
        // 排除当前用户自身，检查其他用户是否已存在相同的 username
        User existingUser = userService.getById(user.getUserID());
        if (existingUser != null && !existingUser.getUsername().equals(user.getUsername()) &&
                userService.isUsernameExists(user.getUsername())) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户名已存在");
        }

        boolean isUpdated = userService.updateById(user);
        return isUpdated ?
                ResponseEntity.ok("用户信息更新成功") :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户信息更新失败");
    }

    /**
     * 根据用户ID删除用户
     */
    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteUserById(@PathVariable("id") String id) {
        boolean isDeleted = userService.removeById(id);
        return isDeleted ?
                ResponseEntity.ok("用户删除成功") :
                ResponseEntity.status(HttpStatus.BAD_REQUEST).body("用户删除失败");
    }
}
